package com.esm.md13.activity;

import static freemarker.log._Log4jOverSLF4JTester.test;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.esm.md13.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends Activity implements View.OnClickListener{

    private final String TAG = "MainActivity";

    public static Intent createIntent(Context context) {
        return new Intent(context, MainActivity.class);
    }

    private static final int PORT = 8023; // 服务器监听的端口号

    private ServerSocket serverSocket;
    private Socket clientSocket;
    private PrintWriter out;
    private BufferedReader in;

    private TextView tv_hege;
    private TextView tv_znum;
    private TextView tv_bad;
    private TextView tv_linename;
    private TextView tv_datetime;
    int _hege = 0;
    int _bad = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.mainactivity);

        tv_hege = (TextView) findViewById(R.id.tv_hege);
        tv_znum = (TextView) findViewById(R.id.tv_znum);
        tv_bad = (TextView) findViewById(R.id.tv_bad);
        tv_linename = (TextView) findViewById(R.id.tv_linename);
        tv_datetime = (TextView) findViewById(R.id.tv_datetime);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    while (true) {
                        Thread.sleep(1000);
                        mHandler.sendEmptyMessage(REFRESH_UI);
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();

        tongyibanben();
    }

    private void tongyibanben() {
        try {
            // 创建一个ServerSocket在指定端口监听
            serverSocket = new ServerSocket(PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    try {
                        Socket socket = serverSocket.accept();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        String line;
                        while ((line = bufferedReader.readLine()) != null) {
                            // 这里可以处理接收到的数据
                           doMsg(line);
                        }
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }

    private void doMsg(String inputLine) {
        String[] parts = inputLine.split("#"); // 使用空格作为分割符
        for (String part : parts) {
            if (part.contains("$")) {
                String strHege = part.replace("$", "");
                try {
                    _hege = Integer.parseInt(strHege);
                } catch (NumberFormatException e) {
                }
            } else {
                try {
                    _bad = Integer.parseInt(part);
                } catch (NumberFormatException e) {
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (serverSocket != null) {
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    private void testA() {
        String inputLine = "$898#2";
        String[] parts = inputLine.split("#"); // 使用空格作为分割符
        for (String part : parts) {
            if (part.contains("$")) {
                String strHege = part.replace("$", "");
                try {
                    _hege = Integer.parseInt(strHege);
                } catch (NumberFormatException e) {
                }
            } else {
                try {
                    _bad = Integer.parseInt(part);
                } catch (NumberFormatException e) {
                }
            }
        }

    }

    public String getCurrentTimeString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    public static final int REFRESH_UI = 1101;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == REFRESH_UI) {
                tv_hege.setText(_hege + "");
                tv_znum.setText((_hege + _bad) + "");
                tv_bad.setText(_bad + "");
//                tv_linename = (TextView) findViewById(R.id.tv_linename);
                tv_datetime.setText(getCurrentTimeString());
            }
        }
    };

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
    }

}

